<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"  xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="centre/manage/include :: header"></head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/css/bootstrap-select.min.css">
<body class="gray-bg">
	<div class="wrapper wrapper-content ">
		<div class="col-sm-12">
			<div class="ibox"><div class="ibox-body">
				<div class="fixed-table-toolbar">
					<div class="columns pull-left col-md-2 nopadding">
						<!-- <label class="control-label">商品搜索关键字：</label> -->
					</div>
					<input type="hidden" name="centreId" id="centreId" th:value="${centreId}" th:if="${flag} == '1'">
					<input type="hidden" name="flag" id="flag" th:value="${flag}">
					<div class="columns pull-left col-md-2 nopadding" th:if="${flag} == '0'">
						<!-- <select data-placeholder="商户" class="form-control chosen-select" name="centreId" id="centreId" tabindex="2">
							<option value="">选择商户</option>
							<option th:each="item: ${centreList}" th:text="${item.name}" th:value="${item.centreId}"></option>
						</select> -->
						  <select id="centreId" name="centreId" class="selectpicker" multiple data-live-search="true"
						     data-actions-box="true">
						      <option value="">选择商户</option>
                                     <option th:each="item: ${centreList}" th:text="${item.name}" th:value="${item.centreId}"></option>   
                           </select>
					</div>
					<div class="columns pull-left col-md-4 nopadding">
						<input type="text" name="mgkey" id="mgkey"
								placeholder="输入商品ID以及名称搜索"
								class="col-sm-6 form-control">
					</div>

					<div class="columns pull-left col-md-1 nopadding">
						<input type="text" class=" form-control"
							   id="upStartTime" name="upStartTime" placeholder="上架开始时间"
							   onclick="laydate({istime: true, format: 'YYYY-MM-DD hh:mm:ss'})"
							   style="background-color: #fff;" readonly="readonly"/>
					</div>
					<div class="columns pull-left">
						<label style="font-weight: 700;float: left;padding: 3px 10px;line-height: 2;font-size: 15px">
							<i class="fa fa-exchange"></i>
						</label>
					</div>
					<div class="columns pull-left col-md-1 nopadding">
						<input type="text" class=" form-control"
							   id="upEndTime" name="upEndTime" placeholder="上架结束时间"
							   onclick="laydate({istime: true, format: 'YYYY-MM-DD hh:mm:ss'})"
							   style="background-color: #fff;" readonly="readonly"/>
					</div>


					<div class="columns pull-left col-md-3 nopadding">
						<span class="input-group-btn">
							<button onclick="reLoad()" type="button"
								class="btn btn-sm btn-primary" style="margin-right:5px;">搜索
							</button>
							
							<button onclick="save()" type="button"
									class="btn btn-sm btn-primary">确定
							</button>
						</span>
					</div>
				</div>
				<table id="selectproductTable" data-mobile-responsive="true"></table>
			</div></div>
		</div>

	</div>
	<div th:include="include::footer"></div>
	<script src="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/js/bootstrap-select.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/js/i18n/defaults-*.min.js"></script>
	<!-- <script src="/js/appjs/product/add.js"></script> -->
</body>
<script>
function closeDow() {
	var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
	parent.layer.close(index);
}

    $('#centreId').selectpicker({
        'selectedText': 'cat',
   	    'noneSelectedText':'请选择商户',
        'deselectAllText':'全不选',
        'selectAllText': '全选',
    });

var prefix = "/product/manage";
var selectProduct = [];
$(function() {
	load();
});
function reLoad() {
	$('#selectproductTable').bootstrapTable('refresh');
}
function load() {
	$('#selectproductTable').bootstrapTable({
		method : 'get', // 服务器数据的请求方式 get or post
		url : prefix + "/allProductByPage", // 服务器数据的加载地址
		// showToggle : true,
		// iconSize : 'outline',
		// toolbar: '#exampleToolbar',
		striped : true, // 设置为true会有隔行变色效果
		dataType : "json", // 服务器返回的数据类型
		pagination : true, // 设置为true会在底部显示分页条
		pageList : [ 10, 20, 50,100,200],
		// queryParamsType : "limit",
		// //设置为limit则会发送符合RESTFull格式的参数
		singleSelect : false, // 设置为true将禁止多选
		// contentType : "application/x-www-form-urlencoded",
		// //发送到服务器的数据编码类型
		pageSize : 10, // 如果设置了分页，每页数据条数
		pageNumber : 1, // 如果设置了分布，首页页码
		// search : true, // 是否显示搜索框
		//showColumns : true, // 是否显示内容下拉框（选择显示的列）
		sidePagination : "server", // 设置在哪里进行分页，可选值为"client" 或者
		// "server"
		queryParams : function(params) {
			return {
				limit : params.limit,
				offset : params.offset,
				idOrName: $("#mgkey").val(),
                upStartTime:$("#upStartTime").val(),
                upEndTime:$("#upEndTime").val(),
			};
		},
		columns : [
			  {
                  checkbox: true
              },
		        {
					field : 'id', // 列字段名
					title : '商品id' // 列标题
				},
				{
					field : 'thumb', // 列字段名
					title : '商品图片', // 列标题
					formatter : function (value, row, index) {
						return "<img src='"+value+"' style='width:50px;height:50px;' />";
					}
				},
				{
					field : 'title',
					title : '商品标题'
				},
				{
					field : 'suggestsaleprice',
					title : '建议零售价'
				},
				{
					field : 'marketprice',
					title : '成本价'
				},
				{
					field : 'timestart',
					title : '销售开始时间'
				},
				{
					field : 'timeend',
					title : '销售结束时间'
				},
				/*{
					field : 'costprice',
					title : '成本价'
				},*/
				{
					field : 'total',
					title : '库存'
				}/* ,
				{
					field : '',
					title : '操作',
					formatter: function (value, row, index) {
						return '<div class="hidden-sm hidden-xs action-buttons" onclick="select_goods(\''+row.id+'\');">' +
						'<a title="添加" ><i style="font-size:130%;color:#DD5A43;" class="fa fa-plus sel'+row.id+'"></i></a>' + 
						'</div>';
					}
				} */]
	});
}

//选择商品
function select_goods(id,title){
	var isHas = false;
	for(var i=0; i<selectProduct.length; i++){
		if(id==selectProduct[i]){
			isHas = true;
			selectProduct.splice(i,1);
		}
	}
	if(isHas){
		$('.sel'+id).removeClass("fa-check fa-check1")
		$('.sel'+id).addClass("fa-plus")
	}else{
		selectProduct.push(id);
		$('.sel'+id).removeClass("fa-plus")
		$('.sel'+id).addClass("fa-check fa-check1")
	}
}

//选择商品保存
function save() {
	 var ids = [];
	 var centreIds = []; 
	 var flag = $("#flag").val();
	 if(flag==1){
		 centreIds[0] = $("#centreId").val();
	 }else{
		 centreIds  = $("#centreId").val(); 
	 }
	 var lock=false;//默认未锁定
	//批量回调
	var rows = $('#selectproductTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
	if (rows.length == 0) {
        layer.msg("请选择要审核通过的数据");
        return;
    }
	if (centreId == null || centreId=='' || centreId == 0) {
		layer.msg("请选择商户", {icon: 5});
		return;
	}
	 // 遍历所有选择的行数据，取每条数据对应的ID
    $.each(rows, function (i, row) {
        ids[i] = row['id'];
    });
	var data = {"centreIds" : centreIds};
	
	/* if (selectProduct == null || selectProduct.length == 0) {
		layer.msg("请选择商品", {icon: 5});
		return;
	} */
	//data.selectProductIds = selectProduct;
	data.selectProductIds = ids;
	layer.confirm("确认要挑选'" + rows.length + "'条商品数据吗?", {
        btn: ['确定', '取消']
        // 按钮
    }, function () {
    	$(".layui-layer-btn0").hide();
    	//防止重复点击
    	layer.open({type:1,title:false,closeBtn:false}) 
    	   if(!lock){
          	 lock=true;//锁定
		    	 $.ajax({
		    		url :'/product/centre/save',
		    		contentType : "application/json",
		    		dataType : "json",
		    		type : 'POST',
		    		data:JSON.stringify(data),
		    		success :function(result){
		    			if (result.code == 0){
		    				parent.layer.msg("保存成功", {icon: 6});
		    				closeDow();
		    			} else {
		    				layer.alert("保存失败，请联系管理员", {icon: 5});
		    			}
		    		}
		    	}) 
    	   }
    });
	
}
</script>
</html>